package com.infoland.afanmall.model.guiGe.adapter;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.model.guiGe.dialog.GuiGeDialogFragment;
import com.infoland.afanmall.model.guiGe.entity.GuiGeEntity;
import com.infoland.afanmall.model.guiGe.linster.CartGuiGeLinster;

import java.util.ArrayList;
import java.util.List;

/****具体规格适配器*/
public class GuiGeAdapter2 extends BaseQuickAdapter<GuiGeEntity.GoodsSpecValListBean, BaseViewHolder> {
    private CartGuiGeLinster linster;
    private List<CheckBox> list;
    private String now_guige;

    public GuiGeAdapter2(int item_cart_guige_cart, List<GuiGeEntity.GoodsSpecValListBean> goodsSpecValList, String now_guige) {
        super(R.layout.item_cart_guige_cart, goodsSpecValList);
        this.list = new ArrayList<>();
        this.now_guige = now_guige;
    }

    @Override
    protected void convert(BaseViewHolder helper, GuiGeEntity.GoodsSpecValListBean item) {
        CheckBox checkBox = helper.getView(R.id.check_lable);
//        LogKit.E("GuiGeAdapter2 ---->   ", "  GuiGeAdapter2");
//        LogKit.E("GuiGeAdapter2 ---->   ", "  " + item.getId());
        int id = item.getId();
        list.add(checkBox);
        GuiGeDialogFragment.listCheckAll.add(checkBox);
        GuiGeDialogFragment.listDataAll.add(item);
        helper.setText(R.id.check_lable, item.getS_value());//
        if (now_guige.contains(id + "")) { //判断是否包含此属性
            checkBox.setChecked(true);
        }
        checkBox.setOnClickListener(v -> {
            startCheck(item, v, helper.getAdapterPosition());
            LogKit.E("list ---->   ", "  " + list.size());
            LogKit.E("listCheckAll ---->   ", "  " + GuiGeDialogFragment.listCheckAll.size());


            for (CheckBox checkBox1 : list) {
                LogKit.E("list ---->   ", "  " + checkBox1.hashCode());
                LogKit.E("list ---->   ", "  " + checkBox1.isChecked());
            }
            for (CheckBox checkBox1 : GuiGeDialogFragment.listCheckAll) {
                LogKit.E("listCheckAll ---->   ", "  " + checkBox1.hashCode());
                LogKit.E("listCheckAll ---->   ", "  " + checkBox1.isChecked());
            }
        });

//        checkBoxes.put("" + id, checkBox);
    }

    /***执行选择事件操作*/
    private void startCheck(GuiGeEntity.GoodsSpecValListBean item, View v, int position) {
        int count = 0;
        for (CheckBox checkBox : list) {
            if (checkBox.isChecked()) {
                count++;
            }
        }
        if (count > 0) {
            /***取消所有按钮***/
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setChecked(false);
            }
            /***选中当前按钮***/
            list.get(position).setChecked(true);
            /****执行计算*/
            linster.goo(item, position);
        } else {
            list.get(position).setChecked(true);
        }
    }

    public void setLinster(CartGuiGeLinster linster) {
        this.linster = linster;
    }
}
